'use strict';

angular.module('voteWithAuthApp')
  .controller('MainCtrl', function ($scope, $http, socket, $state) {
    $scope.meetings = [];
    $scope.session = false;
    //$http.get('/api/meetings/now').success(function(meetings) {
    //  $scope.meetings = meetings;
    //  socket.syncUpdates('meeting', $scope.meetings);
    //});
    $http.get('/api/meetings/now').success(function(meetings) {
      $scope.meetings = meetings;
      if($scope.meetings.length){
        if(window.localStorage['meeting_session_'+meetings[0]._id]) {
          $scope.session = true;
        }
      }
      socket.syncUpdates('meeting', $scope.meetings);
    });


    $scope.join = function () {
      $http.post('/api/meetings/'+ $scope.meetings[0]._id +'/join').success(function() {
        console.log('join..');
        socket.syncUpdates('meeting', $scope.meetings);
        window.localStorage['meeting_session_'+$scope.meetings[0]._id] = true;
        $state.go($state.current, {}, {reload: true});
      });
    };

    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('meeting');
    });
  })

  .controller('MainDashCtrl', function ($scope, $http, socket) {
    $scope.meetings = [];
    $scope.session = false;
    $http.get('/api/meetings/now').success(function(meetings) {
      $scope.meetings = meetings;
      socket.syncUpdates('meeting', $scope.meetings);
    });

    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('meeting');
    });
  })

  .controller('MainVoteCtrl', function ($rootScope, $scope, $http, socket, $stateParams, Modal, filterFilter) {

    $scope.votes = [];
    $scope.meetings = [];
    $scope.alerts = [];
    $rootScope.voted = 0;
    $rootScope.session = false;

    $http.get('/api/meetings/' + $stateParams.meetingId + '/votes').success(function(votes) {
      $scope.votes = votes;
      socket.syncUpdates('vote', $scope.votes);
    });

    $http.get('/api/meetings/'+ $stateParams.meetingId).success(function(meetings) {
      $scope.meetings = meetings;
      socket.syncUpdates('meeting', $scope.meetings);
    });

    $scope.agree = Modal.confirm.agree(function(){});

    $scope.disagree = Modal.confirm.disagree(function(){});

    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('vote');
      socket.unsyncUpdates('meeting');
    });
  })
  .controller('MainDashDetailCtrl', function ($scope, $http, socket, $stateParams, filterFilter) {

    $scope.votes = [];
    $scope.meetings = [];
    $scope.lastvote = {};
    $scope.voted = 0;

    $http.get('/api/meetings/' + $stateParams.meetingId + '/votes').success(function(votes) {
      $scope.votes = votes;

      if(filterFilter($scope.votes, {status:'progress'}).length)
      {
        $scope.myChart = {
          data: {
            labels : ['原则同意', '不同意'],
            datasets : [
              {
                fillColor : '#7eb3eb',
                strokeColor : 'rgba(220,220,220,0.5)',
                data : [filterFilter($scope.votes, {status:'progress'})[0].agree,filterFilter($scope.votes, {status:'progress'})[0].disagree]
              }
            ]
          },
          options: {
            scaleFontSize : 28
          }
        };
      }
      socket.syncUpdates('vote', $scope.votes, function (a, b) {
        $scope.myChart = {
          data: {
            labels : ['原则同意', '不同意'],
            datasets : [
              {
                fillColor : '#7eb3eb',
                strokeColor : 'rgba(220,220,220,0.5)',
                data : [b.agree, b.disagree]
              }
            ]
          },
          options: {animation : false,
            scaleFontSize : 28} };

        if(!filterFilter($scope.votes, {status:'progress'}).length){
          $scope.lastvote = b;
        }
      });
    });

    $http.get('/api/meetings/'+ $stateParams.meetingId).success(function(meetings) {
      $scope.meetings = meetings;
      socket.syncUpdates('meeting', $scope.meetings);
    });


    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('vote');
      socket.unsyncUpdates('meeting');
    });
  })

  .controller('MainDashDetail2Ctrl', function ($scope, $http, socket, $stateParams, filterFilter) {

    $scope.votes = [];
    $scope.meetings = [];
    $scope.voted = 0;
    $scope.bgColor = '#16a085';

    $scope.$watch('bgColor', function(val) {
      $scope.bgColor = val;
      console.log('change...');
      console.log(val);
    });

    $http.get('/api/meetings/' + $stateParams.meetingId + '/votes').success(function(votes) {
      $scope.votes = votes;

      if(filterFilter($scope.votes, {status:'progress'}).length)
      {
        $scope.myChart = {
          data: {
            labels : ['原则同意', '不同意'],
            datasets : [
              {
                fillColor : '#7eb3eb',
                strokeColor : '#bdc3c7',
                data : [filterFilter($scope.votes, {status:'progress'})[0].agree,filterFilter($scope.votes, {status:'progress'})[0].disagree]
              }
            ]
          },
          options: {
            scaleFontSize : 28,
            scaleLineColor : 'rgba(255,255,255,1)',
            scaleGridLineColor : 'rgb(255,255,255)',
            scaleFontColor : '#fff'
          }
        };
      }
      socket.syncUpdates('vote', $scope.votes, function (a, b) {
        $scope.myChart = {
          data: {
            labels : ['原则同意', '不同意'],
            datasets : [
              {
                fillColor : '#7eb3eb',
                strokeColor : '#bdc3c7',
                data : [b.agree, b.disagree]
              }
            ]
          },
          options: {
            animation : false,
            scaleFontSize : 28,
            scaleLineColor : 'rgb(255,255,255)',
            scaleFontColor : '#fff'
          }
        };

        if(!filterFilter($scope.votes, {status:'progress'}).length){
          $scope.vote = b;
        }
      });
    });

    $http.get('/api/meetings/'+ $stateParams.meetingId).success(function(meetings) {
      $scope.meetings = meetings;
      socket.syncUpdates('meeting', $scope.meetings);
    });


    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('vote');
      socket.unsyncUpdates('meeting');
    });
  });
